﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using Common.Logging;
using In.Api.Dto.HisWs;
using log4net.Repository.Hierarchy;
using Newtonsoft.Json;
using OutDbContext;
using User.Api.Dto.Hosp;
using User.Api.Dto.Queue;
using User.Api.HttpClients;

namespace User.Api.Controllers
{
    public class HospController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(QueueController));
        private FsssdqfybjyEntities _ctx;

        protected override void Initialize(HttpControllerContext controllerContext)
        {
            _ctx = new FsssdqfybjyEntities();
            base.Initialize(controllerContext);
        }

        protected override void Dispose(bool disposing)
        {
            _ctx.Dispose();
            base.Dispose(disposing);
        }

        [AllowAnonymous]
        [HttpGet]
        [ActionName("GetHospInfo")]
        public GetHospInfoResponse GetHospInfo()
        {
            var request = new Request4GetHospInfo();

            Response4GetHospInfo response;
            try
            {
                var httpClient = InHttpClient.GetClient();
                var responseMsg = httpClient.PostAsJsonAsync("HisWs/GetHospInfo", request).Result;
                responseMsg.EnsureSuccessStatusCode();
                response = responseMsg.Content.ReadAsAsync<Response4GetHospInfo>().Result;
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用院内GetHospInfo api出错,信息：{0}", JsonConvert.SerializeObject(ex));

                return new GetHospInfoResponse
                {
                    ResultCode = "1",
                    ResultMsg = "服务器或网络异常，请联系客服！"
                };
            }

            if (response.ResultCode != "0")
            {
                return new GetHospInfoResponse
                {
                    ResultCode = "1",
                    ResultMsg = response.ResultDesc
                };
            }

            var hospInfo = response.HospInfo;

            return new GetHospInfoResponse
            {
                ResultCode = "1",
                ResultMsg = "",
                HospId = hospInfo.HospId,
                HospName = hospInfo.HospName,
                ShortName = hospInfo.ShortName,
                Address = hospInfo.Address,
                Tel = hospInfo.Tel,
                WebSite = hospInfo.WebSite,
                Level = hospInfo.Level,
                Desc = hospInfo.Desc
            };
        }

        [HttpGet]
        [ActionName("GetHospBuilding")]
        public GetHospBuildingResponse GetHospBuilding(string branchNo)
        {
            var hospBuildingList = _ctx.HospBuildings.Where(x => x.BranchNo == branchNo && x.RecStatus == 1)
                                                        .OrderBy(x => x.Sort).ToList();

            return new GetHospBuildingResponse()
            {
                ResultCode = "0",
                ResultMsg = "",
                BranchNo = branchNo,
                Buildings = hospBuildingList.Select(x => new GetHospBuildingResponse.Building
                {
                    BuildingId = x.Id,
                    BuildingCode = x.AlphaCode,
                    BuildingName = x.Name,
                }).ToList()
            };
        }

        [HttpGet]
        [ActionName("GetHospFloor")]
        public GetHospFloorResponse GetHospFloor(int buildingId)
        {
            var floorList = _ctx.HospFloors.Where(x => x.BuildingId == buildingId && x.RecStatus == 1)
                                .OrderBy(x => x.Sort).ToList();

            return new GetHospFloorResponse()
            {
                ResultCode = "0",
                ResultMsg = "",
                Floors = floorList.Select(x => new GetHospFloorResponse.Floor
                {
                  Content  = x.Content,
                  Name = x.Name
                }).ToList()
            };
        }
    }
}
